package com.zmx.realm;

import com.zmx.entity.SysUser;
import com.zmx.service.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * 2020/10/19
 * 15:28
 * zmx
 */
public class MyUserRealm extends AuthorizingRealm {
    @Resource
    SysUserService sysUserService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("验证");
        String username = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        SysUser sysUser = sysUserService.SysUserLogin(username);
        if (sysUser == null) {
            throw new UnknownAccountException("无此账号");
        }
        if (!password.equals(sysUser.getPassword())){
            throw new  IncorrectCredentialsException("密码错误");
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(sysUser,password,this.getName());
        return simpleAuthenticationInfo;
    }
}
